using System;
using System.Runtime.InteropServices;
using LitJson2;
using UnityEngine;
using Object = UnityEngine.Object;

/****************************************************
// 功能：平台消息管理器
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/1/7 10:10:12
*****************************************************/

public class PlatformMsgManager : SingletonPatternBase<PlatformMsgManager>
{
    public GameObject platformObject = null;
    public PlatformScript platformScript = null;

    public void Init()
    {
        
        if (platformObject == null)
        {
            platformObject = new GameObject("PlatformObject")
            {
                hideFlags = HideFlags.HideAndDontSave
            };
            platformScript = platformObject.AddComponent<PlatformScript>();

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return;
            }
#endif
            Debug.Log("PlatformMsgManager Init");
            
            Object.DontDestroyOnLoad(platformObject);
        }

#if UNITY_ANDROID && !UNITY_EDITOR
        gameHelperJavaClass = new AndroidJavaClass("liaolaiapp.com.app.GameHelper");
        Debug.Log("Platform Init liaolaiapp.com.app.GameHelper");
#endif
    }

#if UNITY_ANDROID && !UNITY_EDITOR
    private AndroidJavaClass gameHelperJavaClass = null;

    /// <summary>
    /// 从Unity发送消息到Android平台
    /// </summary>
    public void SendUnityMessageToPlatform(int iMsgId, int iParam1 = 0,int iParam2 = 0,int iParam3 = 0, int iParam4 = 0, string strParam1 = "", string strParam2 = "", string strParam3 = "", string strParam4 = "")
    {
        gameHelperJavaClass?.CallStatic("SendUnityMessageToPlatform", iMsgId, iParam1, iParam2, iParam3, iParam4, strParam1, strParam2, strParam3, strParam4);
    }
    
    /// <summary>
    /// 从平台获取字符串数据
    /// </summary>
    public string GetStringFormPlatform(int type)
    {
        if (gameHelperJavaClass == null) return String.Empty;

        return gameHelperJavaClass.CallStatic<string>("GetStringFormPlatform", type);
    }
#elif UNITY_IOS && !UNITY_EDITOR
    /// <summary>
    /// 从Unity发送消息到IOS平台
    /// </summary>
    public void SendUnityMessageToPlatform(int iMsgId, int iParam1 = 0,int iParam2 = 0,int iParam3 = 0, int iParam4 = 0, string strParam1 = "", string strParam2 = "", string strParam3 = "", string strParam4 = "")
    {
        _SendUnityMessageToPlatform(iMsgId, iParam1, iParam2, iParam3, iParam4, strParam1, strParam2, strParam3, strParam4);
    }

    
    /// <summary>
    /// 从平台获取字符串数据
    /// </summary>
    public string GetStringFormPlatform(int type)
    {
        return _GetStringFromPlatform(type);
    }

    [DllImport("__Internal")]
    public static extern void _SendUnityMessageToPlatform(int iMsgId, int iParam1 = 0, int iParam2 = 0, int iParam3 = 0, int iParam4 = 0, string strParam1 = "", string strParam2 = "", string strParam3 = "", string strParam4 = "");


    [DllImport("__Internal")]
    public static extern string _GetStringFromPlatform(int type);
#else
    /// <summary>
    /// 从Unity发送消息到Android平台
    /// </summary>
    public void SendUnityMessageToPlatform(int iMsgId, int iParam1 = 0,int iParam2 = 0,int iParam3 = 0, int iParam4 = 0, string strParam1 = "", string strParam2 = "", string strParam3 = "", string strParam4 = "")
    {
    }

    /// <summary>
    /// 从平台获取字符串数据
    /// </summary>
    public string GetStringFormPlatform(int type)
    {
        return string.Empty;
    }
#endif
    
}
